package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiredPermission(name = "消费单明细页面", expression = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }


    @RequiredPermission(name = "消费单明细删除", expression = "consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id,Long[] ids) {
        if (id != null) {

            consumptionService.delete(id);
        }
        return "redirect:/consumption/input";
    }


    @RequiredPermission(name = "消费单明细新增/编辑", expression = "consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }

    @RequestMapping("/save")
    public String saveFormAppointment(Long appointmentId) {
        //在service中,修改预约单的状态,新增结算单,返回结算单对象
        Long consumptionId = consumptionService.saveFormAppointment(appointmentId);

        return "redirect:/consumption/input?id=" + consumptionId;
    }


    @RequiredPermission(name = "消费单明细新增/编辑", expression = "consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("businesses",businessService.listAll());
        model.addAttribute("categoryItems",systemDictionaryItemService.queryBySn("business"));
        model.addAttribute("payTypeItems",systemDictionaryItemService.queryBySn("pay_type"));
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption", consumption);
            //根据结算单号查询结算单明细
           List<ConsumptionItem> items = consumptionItemService.queryByCon(consumption.getCno());
           model.addAttribute("items",items);

        }
        return "consumption/input";
    }
}
